app.controller('countersign', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //详情页
    $scope.confirmsign = function(){
        $state.go(confirmsign.stateName);
    }


    $scope.choose = function(index){
        if(1==index){  //选中 “现在签字”
            $scope.c = 1;
            $(".now").css("display","block");
            $(".shot").css("display","none");
        }else{ //拍照上传
            $scope.c = 2;
            $(".now").css("display","none");
            $(".shot").css("display","block");
        }
    }

    //拍照
    $scope.takePhoto = function () {
        if($scope.imageUrls.length >= 1){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传1张图片"));
            return;
        }
        var scanType = "sdmSignImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
            //context.uploadImage(getToken(),scanType);
			window.webkit.messageHandlers.uploadImage.postMessage({"scanType":scanType});

//            var token = getToken();
//            var reqeustData = {};
//            reqeustData.username = "support@jbkj.net.cn";
//            reqeustData.username = "13691179771@163.com";
//            reqeustData.password = "123456";
//
//            var url = "http://183.195.106.118:8150/fec-api/api/login";
//            // var url = "https://cnmpse2e-itg.ext.hp.com/fec-api/api/login";
//            postHttp2($http, url, reqeustData, function (res) {
//                if (res.msg === 'success') {
//                    token = res.token;
//                    context.uploadImage(token,scanType);
//                }
//            });
        }else{
			 hm.uploadImage(getToken(),scanType);
		}
    }
    //存入数组图片路径
    $scope.photoResult = function (url) {
        // alert(JSON.stringify(url));
        /*if(typeof(url)=='string'){
            $scope.imageUrls.push(JSON.parse(url).imageurl);
        }else{
            $scope.imageUrls.push(url.imageurl);
        }
        $scope.$applyAsync();*/
		if(typeof(url)=='string'){
		   var jsonData = JSON.parse(url).imageurl;
		   for (var i = 0; i < jsonData.length; i++) {
		       var counter = jsonData[i].url;
		   	 $scope.imageUrls.push(counter);
		   }
		}else{
		    $scope.imageUrls.push(url.imageurl);
		}
		$scope.$applyAsync();
    }

    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }


    //获得数据
    $scope.submit = function () {
        // alert(0);

        if($scope.c==2){
            // alert(JSON.stringify($scope.imageUrls));
            if(!$scope.imageUrls || $scope.imageUrls.length==0){
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请先拍照"));
                return false;
            }

            $scope.customerSignUrl = $scope.imageUrls[0];
            //存签名
            localStorage.setItem("sdm-countcustomerSignUrl", $scope.customerSignUrl);

            $scope.confirmsign();
        }else {
            // alert(2);
            if ($("#signature").jSignature("getData", "native").length == 0) {
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
                return false;
            }
            importSign('signature', 'signature', function (result) {
				var json = JSON.stringify(result)
				var jsonData = JSON.parse(json).obj;
				for (var i = 0; i < jsonData.length; i++) {
				    url = jsonData[i].url;
				}
                if (null == url) {
                    $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
                    return;
                }
                if (1 == $scope.signType) {
                    $scope.customerSignUrl = url;
                    //存签名
                    localStorage.setItem("sdm-countcustomerSignUrl", url);
                } else if (2 == $scope.signType) {
                    $scope.feSignUrl = url;
                    //存签名
                    localStorage.setItem("sdm-countfeSignUrl", url);
                }
                $scope.confirmsign();
            });
        }
    }

    //初始化
    function init(){
        $scope.feSignUrl = "";
        $scope.customerSignUrl = "";
        $scope.c = "1";

        //客户签字的图片
        $scope.imageUrls = [];

        //签名类型
        $scope.signType = countersign.params.type;
        $scope.signTypeName = "";
        if(1==$scope.signType){
            $scope.signTypeName = "客户";

            $(".choose").css("display","flex");
        }else{
            $scope.signTypeName = "工程师";

            $(".choose").css("display","none");
        }
    }

    init();


    //初始化画布
    $(document).ready(function(){
        var innnerheight = window.innerHeight;
        var height=(innnerheight - 250 )+'px';
        document.getElementById("sign").style.height = height;
        var width=100+'%';
        $("#signature").jSignature({height:height,width:width,color:"#000",lineWidth:4});
    })
    //重置画布
    $scope.clearSig = function(){
        $("#signature").jSignature("clear");
    }

    reloadJs();

});

//返回扫描结果
function sdmSignImageResult(msg) {
    // alert(msg);
    var $scope = getScope("countersign");
    $scope.photoResult(msg);
}